<?php

namespace app\admin\controller\ques;

use app\common\controller\Backend;

/**
 * 问卷调查-答题管理
 *
 * @icon fa fa-circle-o
 */
class Volume extends Backend
{

    /**
     * Volume模型对象
     * @var \app\admin\model\ques\Volume
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ques\Volume;

    }
    public function edit($ids = NULL)
    {
        ini_set('max_execution_time', 600);
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    if ($params['veri']=='1'){
                        if ($params['verifile']=='')$this->error("请上传小鹅通数据");
                        $this->imports($params['verifile'],$params['end_time'],$ids);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    private function imports($file,$endtime,$ids)
    {
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new \PHPExcel_Reader_CSV();
                if (!$PHPReader->canRead($filePath)) {
                    $this->error(__('Unknown data format'));
                }
            }
        }
        $PHPExcel = $PHPReader->load($filePath); //加载文件
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
        $redis = getRedis();
        $time = strtotime($endtime)-time();
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $cname = $currentSheet->getCellByColumnAndRow(6, $currentRow)->getValue();//家长姓名
            $name = $currentSheet->getCellByColumnAndRow(7, $currentRow)->getValue();

            $mobile = trim(str_replace(" ","",$currentSheet->getCellByColumnAndRow(8, $currentRow)->getValue()));
            $keys='xt_'.$ids.'_'.$mobile;
            $redis->set($keys,$cname.'|'.$name);
            $redis->expire($keys,$time);
        }
        return true;

    }

}